import java.util.Arrays;
//定义了一个接口Route
interface Route {
    //接口中的抽象方法
    public abstract double getFare();
    double getDistance();
    void addStation(String station);
    void removeStation(String station);
    void updateStation(String oldStation,String newStation);
    void queryStations();
}
abstract class AbstractRoute implements Route{
    private String name;
    public  String[] stations;
    private String startStation;
    private String endStation;
    public AbstractRoute(String name, String[] stations, String startStation, String endStation) {
        this.name = name;
        this.stations = stations;
        this.startStation = startStation;
        this.endStation = endStation;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getStations() {
        return stations;
    }

    public void setStations(String[] stations) {
        this.stations = stations;
    }

    public String getStartStation() {
        return startStation;
    }

    public void setStartStation(String startStation) {
        this.startStation = startStation;
    }

    public String getEndStation() {
        return endStation;
    }

    public void setEndStation(String endStation) {
        this.endStation = endStation;
    }
}
class SpecificRoute extends AbstractRoute{
    public SpecificRoute(String name, String[] stations, String startStation, String endStation) {
        super(name, stations, startStation, endStation);
    }
    @Override
    public double getFare() {
        return stations.length*1;
    }
    @Override
    public double getDistance() {
        return stations.length*2;
    }
    //增加数据信息
    @Override
    public void addStation(String station){
        System.out.println("要增加的站点信息为:"+station);
        int index=0;
        while ((index<stations.length)){
            index++;
        }
        if(index<stations.length){
            stations[index]=station;
        }
        else{
            stations= Arrays.copyOf(stations,stations.length+1);
            stations[index]=station;
        }
    }
    //删除数据信息
    @Override
    public void removeStation(String station) {
        System.out.println("要删除的站点为:"+station);
        boolean find=false;
        for(int i=0;i<stations.length;i++){
            if(stations[i].equals(station)){
                find = true;
                int j=i;
                for (; j < stations.length-1; j++) {
                    stations[j]=stations[j+1];
                }

            }

        }
        if(find){
            stations=Arrays.copyOf(stations,stations.length-1);
        }

    }
    //更改数据信息
    @Override
    public void updateStation(String oldStation, String newStation) {
        System.out.println("要更改的站点信息为:"+oldStation+"->"+newStation);
        for (int i = 0; i < stations.length; i++) {
            if(stations[i].equals(oldStation)){
                stations[i]=newStation;
                break;
            }
        }
    }
    //查找公交站点信息
    @Override
    public void queryStations() {
        System.out.println(getName() + "的站点有：");
        for (int i = 0; i < stations.length; i++) {
            System.out.print(stations[i]+" ");
        }

        System.out.println();
    }

}
public class Test {
    public static void main(String[] args) {
        AbstractRoute abstractRoute=new SpecificRoute("101",new String[]{"S1","S2","S3","S4","S5"},
                "S1","S5");
        abstractRoute.queryStations();
        abstractRoute.addStation("S6");
        abstractRoute.queryStations();
       abstractRoute.removeStation("S2");
        abstractRoute.queryStations();
        abstractRoute.updateStation("S3","S7");
        abstractRoute.queryStations();
        System.out.println("路程为："+ abstractRoute.getDistance()+"公里"+"  "+"费用为"+abstractRoute.getFare()+"元");
    }
}
